package cn.qiyu5522.tk_ems.controller.admin;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.qiyu5522.tk_ems.entity.Company;
import cn.qiyu5522.tk_ems.entity.dto.CompanyDTO;
import cn.qiyu5522.tk_ems.entity.dto.QueryParamDTO;
import cn.qiyu5522.tk_ems.myanotation.WebLog;
import cn.qiyu5522.tk_ems.result.R;
import cn.qiyu5522.tk_ems.service.CompanyService;
import cn.qiyu5522.tk_ems.utils.RedisKey;
import cn.qiyu5522.tk_ems.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Tonkey
 * @since 2023-02-19
 */
@Api(tags = "企业管理")
@RestController
@RequestMapping("admin/company")
@SaCheckPermission(orRole = {"admin", "company"})
public class CompanyController {
    @Resource
    private CompanyService companyService;
    @Resource
    private RedisUtil redisUtil;

    @WebLog(description = "分页条件查询企业列表")
    @ApiOperation("企业查询")
    @ApiImplicitParam(name = "queryParamDTO", value = "条件查询对象", required = true, dataType = "QueryParamDTO", dataTypeClass = QueryParamDTO.class)
    @PostMapping("/query")
    public R query(@Validated(QueryParamDTO.Query.class) @RequestBody QueryParamDTO<CompanyDTO> queryParamDTO) {
        return companyService.queryCompanyByPageCondition(queryParamDTO);
    }


    @WebLog(description = "企业新增--批量")
    @ApiOperation("企业新增")
    @ApiImplicitParam(name = "companyDTOS", value = "企业新增", required = true, dataType = "List", dataTypeClass = List.class)
    @PostMapping("/add")
    public R add(@Validated(Company.Insert.class) @RequestBody List<CompanyDTO> companyDTOS) {
        return companyService.saveMore(companyDTOS);
    }

    @WebLog(description = "企业编辑")
    @ApiOperation("企业编辑")
    @ApiImplicitParam(name = "company", value = "企业实体类对象", required = true, defaultValue = "{}", dataTypeClass = Company.class)
    @PutMapping("/edit")
    public R edit(@Validated @RequestBody Company company) {
        boolean b = companyService.updateById(company);
        if (b) {
            // 编辑成功，刷新Redis中的缓存
            // 查询全部的企业数据
            QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("id", "name", "code");
            List<Company> departmentList = companyService.list(queryWrapper);
            redisUtil.set(RedisKey.COMPANY_ID_AND_NAME_AND_CODE, departmentList, RedisKey.EXPIRE_ONE_DAY);
        }
        return b ? R.ok().message("编辑成功") : R.error().message("编辑失败");
    }

    @WebLog(description = "企业删除")
    @ApiOperation("企业删除")
    @ApiImplicitParam(name = "ids", value = "企业编号集合", required = true, defaultValue = "[]", dataTypeClass = List.class)
    @PostMapping("/remove")
    public R remove(@RequestBody List<Long> ids) {
        return companyService.removeMore(ids);
    }

    @ApiOperation("查询全部企业编号与名称、编码")
    @GetMapping("/queryForIdAndNameAndCode")
    public R queryForIdAndNameAndCode() {
        List<Company> forIdAndNameAndCode = companyService.queryForIdAndNameAndCode();
        return R.ok().data("companiesForIdAndNameAndCode", forIdAndNameAndCode);
    }

    @ApiOperation("检测企业是否重复")
    @ApiImplicitParam(name = "code", value = "企业编码", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/queryForCheckRepeatName/{code}")
    public R queryForCheckRepeatName(
            @NotBlank(message = "根据名称查询专业时,专业编号不能为空") @PathVariable("code") Long code) {
        return companyService.queryByCode(code);
    }

    @ApiOperation("获取全部公司名称与编号、编码")
    @GetMapping("/queryForNameAndIdAndCode")
    public R queryForNameAndIdAndCode() {
        return companyService.queryForNameAndIdAndCode();
    }

    @ApiOperation("根据ID查询企业信息")
    @ApiImplicitParam(name = "id", value = "企业ID", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/queryCompanyInfoByID/{id}")
    public R queryCompanyInfoById(
            @NotBlank(message = "根据企业ID查询企业信息时,ID不能为空") @PathVariable("id") Long id) {
        return companyService.queryCompanyInfoById(id);

    }

}
